using System;

namespace SBPweb.Modules.Dependencies
{
	/// <summary>
	/// Class of VersionMask
	/// </summary>
    public class VersionMask
    {
        #region Private fields

        private int? pMajor;
        private int? pMinor;
        private int? pRevision;
        private int? pBuild;

        #endregion

        #region Contstructor logic

		/// <summary>
		/// Constructor of VersionMask
		/// </summary>
		/// <param name="version"></param>
        public VersionMask(string version)
        {
            string[] split = version.Split(new char[] { '.' }, StringSplitOptions.None);

            if (split.Length > 1)
            {
                pMajor = TryGetInt(split[0]);
                pMinor = TryGetInt(split[1]);
            }
            if (split.Length > 2)
            {
                pRevision = TryGetInt(split[2]);
            }
            if (split.Length > 3)
            {
                pBuild = TryGetInt(split[3]);
            }

        }

        #endregion

        #region Property accessors

		/// <summary>
		/// Major number
		/// </summary>
        public int? Major
        {
            get
            {
                return pMajor;
            }
        }

		/// <summary>
		/// Minor number
		/// </summary>
        public int? Minor
        {
            get
            {
                return pMinor;
            }
        }

		/// <summary>
		/// Revision number
		/// </summary>
        public int? Revision
        {
            get
            {
                return pRevision;
            }
        }

		/// <summary>
		/// Bild number
		/// </summary>
        public int? Build
        {
            get
            {
                return pBuild;
            }
        }

        #endregion

        #region Public methods

		/// <summary>
		/// ToString
		/// </summary>
		/// <returns>string</returns>
        public override string ToString()
        {
            return string.Format("{0}.{1}.{2}.{3}", pMajor.HasValue ? pMajor.Value.ToString() : "*", pMinor.HasValue ? pMinor.Value.ToString() : "*", pRevision.HasValue ? pRevision.Value.ToString() : "*", pBuild.HasValue ? pBuild.Value.ToString() : "*");
        }

		/// <summary>
		/// Is met?
		/// </summary>
		/// <param name="version"></param>
		/// <returns></returns>
        public bool IsMet(System.Version version)
        {
			return !(
				(pMajor.HasValue && pMajor.Value != version.Major)
				||
				(pMinor.HasValue && pMinor.Value != version.Minor)
				||
				(pRevision.HasValue && pRevision.Value != version.Revision)
				||
				(pBuild.HasValue && pBuild.Value != version.Build)
				);
        }

        #endregion

        #region Private helper functions

        private int? TryGetInt(string s)
        {
            try
            {
                if (s == "*")
                {
                    return null;
                }
                return Convert.ToInt16(s);
            }
            catch
            {
                return null;
            }
        }

        #endregion
    }
}
